package com.atguigu.datastream.day07;

import com.atguigu.datastream.bean.Event;
import com.atguigu.datastream.test.day03.Flink_05_Source_useDefault;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.ProcessFunction;
import org.apache.flink.util.Collector;
import org.apache.flink.util.OutputTag;

/**
 * ClassName: Flink03_SplitStreamByFilter_WithSideOutput
 * Package: com.atguigu.day07
 * Description:
 *             使用侧输出流将一条流根据不同的用户拆分成多条流
 * @Author ChenJun
 * @Create 2023/4/14 16:38
 * @Version 1.0
 */
public class Flink03_SplitStreamByFilter_WithSideOutput {
    public static void main(String[] args) throws Exception {

        //1. 创建流的运行环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        env.setParallelism(1);

        //2. 读取数据
        DataStreamSource<Event> streamSource = env.addSource(new Flink_05_Source_useDefault.ClickSource());

        //TODO 3. 使用侧输出流将一条流根据不同的用户拆分成多条流
        SingleOutputStreamOperator<Event> result = streamSource.process(new ProcessFunction<Event, Event>() {
            @Override
            public void processElement(Event value, ProcessFunction<Event, Event>.Context ctx, Collector<Event> out) throws Exception {
                if ("Bingbing".equals(value.user)) {
                   ctx.output(new OutputTag<Event>("Bingbing"){},value);
                } else {
                    out.collect(value);
                }
            }
        });

        result.print("Songsong");
        //获取到侧输出流的数据
        result.getSideOutput(new OutputTag<Event>("Bingbing"){}).print("bingbing");

        env.execute();

    }
}
